package com.falko.android.raven.settings;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.falko.android.raven.R;

public class RadioButtonPreference extends Preference implements
		OnCheckedChangeListener {

	public final int BUTTON_ONE = 0;
	public final int BUTTON_TWO = 1;
	public final int BUTTON_THREE = 2;
	public final int BUTTON_FOUR = 3;

	private final String TAG = getClass().getName();
	private int userSelection_ = 0;
	private LinearLayout layout_ = null;
	private String buttonText[];
	private int buttonIDs_[];
	private String title_ = "";
	private String summary_ = "";
	private String orientation_ = "vertical";
	private int buttonCount_ = 0;

	public RadioButtonPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		initPreference(context, attrs);
	}

	public RadioButtonPreference(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		initPreference(context, attrs);
	}

	private void initPreference(Context context, AttributeSet attrs) {

		TypedArray a = getContext().obtainStyledAttributes(attrs,
				R.styleable.RadioGroup);
		buttonIDs_ = new int[4];
		buttonText = new String[4];
		buttonText[BUTTON_ONE] = a
				.getString(R.styleable.RadioGroup_buttonOneText);
		buttonText[BUTTON_TWO] = a
				.getString(R.styleable.RadioGroup_buttonTwoText);
		buttonText[BUTTON_THREE] = a
				.getString(R.styleable.RadioGroup_buttonThreeText);
		buttonText[BUTTON_FOUR] = a
				.getString(R.styleable.RadioGroup_buttonFourText);
		title_ = a.getString(R.styleable.RadioGroup_title);
		summary_ = a.getString(R.styleable.RadioGroup_summary);
		buttonCount_ = a.getInt(R.styleable.RadioGroup_buttonCount, 4);
		orientation_ = a.getString(R.styleable.RadioGroup_orientation);

		a.recycle();
	}

	@Override
	protected View onCreateView(ViewGroup parent) {

		layout_ = null;

		try {
			LayoutInflater mInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			layout_ = (LinearLayout) mInflater.inflate(
					R.layout.radio_button_preference, parent, false);
		} catch (Exception e) {
			Log.e(TAG, "Error creating preference", e);
		}

		((TextView) layout_.findViewById(R.id.title)).setText(title_);
		((TextView) layout_.findViewById(R.id.summary)).setText(summary_);

		RadioButton buttons[] = {
				(RadioButton) layout_.findViewById(R.id.radio_one),
				(RadioButton) layout_.findViewById(R.id.radio_two),
				(RadioButton) layout_.findViewById(R.id.radio_three),
				(RadioButton) layout_.findViewById(R.id.radio_four) };

		for (int i = 0; i < buttonCount_; ++i) {
			buttons[i].setVisibility(View.VISIBLE);
			buttons[i].setText(buttonText[i]);
			buttonIDs_[i] = buttons[i].getId();

			if (userSelection_ == i) {
				buttons[i].setChecked(true);
			}
		}

		if (orientation_.equalsIgnoreCase("horizontal")) {
			((RadioGroup) layout_.findViewById(R.id.radio_group))
					.setOrientation(RadioGroup.HORIZONTAL);
		}
		((RadioGroup) layout_.findViewById(R.id.radio_group))
				.setOnCheckedChangeListener(this);
		layout_.invalidate();

		return layout_;
	}

	@Override
	protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

		if (restoreValue) {
			userSelection_ = getPersistedInt(userSelection_);
		} else {
			int temp = 0;
			persistInt(temp);
			userSelection_ = temp;
		}
	}

	public void onCheckedChanged(RadioGroup group, int checkedId) {

		int selection = -1;
		if (checkedId == buttonIDs_[BUTTON_ONE]) {
			selection = BUTTON_ONE;
		} else if (checkedId == buttonIDs_[BUTTON_TWO]) {
			selection = BUTTON_TWO;
		} else if (checkedId == buttonIDs_[BUTTON_THREE]) {
			selection = BUTTON_THREE;
		} else if (checkedId == buttonIDs_[BUTTON_FOUR]) {
			selection = BUTTON_FOUR;
		}

		if (selection != -1 && !callChangeListener(selection)) {
			return;
		}

		userSelection_ = selection;
		persistInt(userSelection_);
		notifyChanged();
	}

}
